package com.wmx.Dbutils;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

/**
 * @author:wmx 2025-03-28 15:36
 *功能:反射工具类
 *
 **/
public class DbutilClass {
    /**
     *字段:设置字段的值
     * @param getclass
     * @param field
     * @param parameter
     * @param <T>
     * @param <E>
     * @throws Exception
     */
    public static <T,E> void setFiled(T getclass, String field,E parameter) throws Exception {
        Class<?> aClass = getclass.getClass();
        Field studentSex = aClass.getDeclaredField(field);
        studentSex.setAccessible(true);
        studentSex.set(getclass,parameter);
    }

    /**
     * 获取类中注解中value()的内容
     * @param getclass 类
     * @param annotationClass 注解.class对象
     * @param <T>
     * @param <E>
     * @return 返回value()方法的值
     * @throws Exception
     */
    public static <T , E extends Annotation> Object  getClassAnnotation(Class<T> getclass, Class<E> annotationClass) throws Exception {

        boolean annotationPresent = getclass.isAnnotationPresent(annotationClass);
        System.out.println(annotationPresent);
        if(!annotationPresent){
            throw new Exception("没有注解");
        }else{
            E annotation = getclass.getAnnotation(annotationClass);

            return annotation.annotationType().getMethod("value").invoke(annotation);


        }
    }

    /**
     *
     获取字段注解中value()的内容
     * @param annotationClass 注解.class对象
     * @param field 字段
     * @param <E>
     * @return 返回value()方法的值
     * @throws Exception
     */
    public static <E extends Annotation> Object  getFieldAnnotation(Field field, Class<E> annotationClass) throws Exception {

        boolean annotationPresent = field.isAnnotationPresent(annotationClass);
        System.out.println(annotationPresent);
        if(!annotationPresent){
            throw new Exception("没有注解");
        }else{
            E annotation = field.getAnnotation(annotationClass);
            return annotation.annotationType().getMethod("value").invoke(annotation);


        }
    }
}
